// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 


using SoapTrace.Core.Communication;
using System;

namespace SoapTrace.Tests.Utils
{
	/// <summary>
	/// Data packet utility class.
	/// </summary>
	public class PacketBytes
	{
		/// <summary>
		/// <para>Gets a byte array from an ascii string of hex numbers.  
		/// The string has the format:</para>
		/// <para>
		/// <code>
		/// 0000 AA BB CC DD 01 02 03 04  BB CC BB CC 01 02 02 01 
		/// 0010 DD EE
		/// </code>
		/// </para>
		/// </summary>
		/// <param name="data"></param>
		/// <returns></returns>
		public static byte[ ] GetBytesFromString( string data )
		{
			ByteArray Bytes = new ByteArray( );

			// Split the items.

			string[ ] DataItems = data.Split( ' ', '\n' );

			// Extract all bytes from the split items.
				
			for( int i = 0; i < DataItems.Length; ++i )
			{
				string Item = DataItems[ i ].Trim( );

				if( Item.Length == 2 )
				{
					byte ByteToAdd = Convert.ToByte( Item, 16 );
					Bytes.Add( ByteToAdd );
				}
				else
				{
					// Ignore item, it is probably the byte count at the 
					// start of the line.
				}
			}

			return Bytes.GetBytes( );
		}
	}
}
